/*
 * CItemChooser.java
 *
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 *
 * This file is part of JRogueDPS.
 *
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package paperdoll;

import geardatabase.CItem;
import geardatabase.CItemComparatorQualityAlpha;
import geardatabase.CGemType;
import geardatabase.CItemQuality.ItemQuality;
import geardatabase.CItemSlot.ItemSlot;
import geardatabase.CItemType.ItemType;
import geardatabase.CQualityFilter;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JCheckBox;
import jroguedps.CGlobal;
import paperdoll.CDollSlotType.DollSlotType;

/**
 *
 * @author Mani
 */
public class CItemChooser extends javax.swing.JDialog {

    public enum ChooserType {META, GEM, ENCHANT, ITEM, POISON};
    private CQualityFilter m_gemQualityFilter;
    private CQualityFilter m_itemQualityFilter;
    private ChooserType m_chooserType;
    private DollSlotType m_slotType;
    private CItem m_itemChosen = null;

    /** Creates new form CItemChooser */
    public CItemChooser(java.awt.Frame parent, DollSlotType p_slotType, ChooserType p_itemType,
                            CQualityFilter p_gemQualityFilter, CQualityFilter p_itemQualityFilter) {
        super(parent, p_slotType.toString() + " " + p_itemType.toString(), true);
        initComponents();
        this.setLocationRelativeTo(parent);
        
        m_gemQualityFilter = p_gemQualityFilter;
        m_itemQualityFilter = p_itemQualityFilter;
        m_chooserType = p_itemType;
        m_slotType = p_slotType;
        populateColumns();
        populateTable();
        
        if (p_itemType == ChooserType.META ||
            p_itemType == ChooserType.ENCHANT ||
            p_itemType == ChooserType.POISON)
        {
            this.legendaryCheckBox.setEnabled(false);
            this.epicCheckBox.setEnabled(false);
            this.rareCheckBox.setEnabled(false);
            this.uncommonCheckBox.setEnabled(false);
            this.commonCheckBox.setEnabled(false);
        } else if (p_itemType == ChooserType.GEM)
        {
            this.legendaryCheckBox.setEnabled(false);
            this.epicCheckBox.setSelected(p_gemQualityFilter.isEpic());
            this.rareCheckBox.setSelected(p_gemQualityFilter.isRare());
            this.uncommonCheckBox.setSelected(p_gemQualityFilter.isUncommon());
            this.commonCheckBox.setSelected(p_gemQualityFilter.isCommon());
        }else if (p_itemType == ChooserType.ITEM)
        {
            this.legendaryCheckBox.setSelected(p_itemQualityFilter.isLegendary());
            this.epicCheckBox.setSelected(p_itemQualityFilter.isEpic());
            this.rareCheckBox.setSelected(p_itemQualityFilter.isRare());
            this.uncommonCheckBox.setSelected(p_itemQualityFilter.isUncommon());
            this.commonCheckBox.setSelected(p_itemQualityFilter.isCommon());
        }
    }

    private void populateColumns()
    {
        CItemChooserTableModel l_tableModel = (CItemChooserTableModel) this.itemChooserTable.getModel();
        this.itemChooserTable.setDefaultRenderer(CItem.class, new CItemChooserRenderer(true));
        System.out.println("Slot Type " + m_slotType);
        if (m_chooserType == ChooserType.META)
        {
            l_tableModel.addColumn("Meta");

        } else if (m_chooserType == ChooserType.GEM)
        {
            l_tableModel.addColumn("Red");
            l_tableModel.addColumn("Blue");
            l_tableModel.addColumn("Green");
            l_tableModel.addColumn("Orange");
            l_tableModel.addColumn("Purple");
            l_tableModel.addColumn("Yellow");
            l_tableModel.addColumn("Prismatic");


        } else if (m_chooserType == ChooserType.ENCHANT)
        {
          l_tableModel.addColumn("Enchant");
        } else if (m_chooserType == ChooserType.POISON)
        {
          l_tableModel.addColumn("Poison");
        } else if (m_chooserType == ChooserType.ITEM)
        {
            if (m_slotType == DollSlotType.HEAD)
            {
                l_tableModel.addColumn("Head");
            } else if (m_slotType == DollSlotType.NECK)
            {
                l_tableModel.addColumn("Neck");
            } else if (m_slotType == DollSlotType.SHOULDERS)
            {
                l_tableModel.addColumn("Shoulders");
            } else if (m_slotType == DollSlotType.BACK)
            {
                l_tableModel.addColumn("Back");
            } else if (m_slotType == DollSlotType.CHEST)
            {
                l_tableModel.addColumn("Chest");
            } else if (m_slotType == DollSlotType.WRIST)
            {
                l_tableModel.addColumn("Wrists");
            } else if (m_slotType == DollSlotType.HANDS)
            {
                l_tableModel.addColumn("Hands");
            } else if (m_slotType == DollSlotType.WAIST)
            {
                l_tableModel.addColumn("Waist");
            } else if (m_slotType == DollSlotType.LEGS)
            {
                l_tableModel.addColumn("Legs");
            } else if (m_slotType == DollSlotType.FEET)
            {
                l_tableModel.addColumn("Boots");
            } else if (m_slotType == DollSlotType.FINGER1 ||
                       m_slotType == DollSlotType.FINGER2)
            {
                l_tableModel.addColumn("Ring");
            } else if (m_slotType == DollSlotType.TRINKET1 ||
                       m_slotType == DollSlotType.TRINKET2)
            {
                l_tableModel.addColumn("Trinket");
           } else if (m_slotType == DollSlotType.RANGED)
            {
                l_tableModel.addColumn("Bow");
                l_tableModel.addColumn("Crossbow");
                l_tableModel.addColumn("Gun");
                l_tableModel.addColumn("Thrown");
            } else if (m_slotType == DollSlotType.MAINHAND)
            {
                l_tableModel.addColumn("MH Axe");
                l_tableModel.addColumn("MH Dagger");
                l_tableModel.addColumn("MH Fist");
                l_tableModel.addColumn("MH Mace");
                l_tableModel.addColumn("MH Sword");
            } else if (m_slotType == DollSlotType.OFFHAND)
            {
                l_tableModel.addColumn("OH Axe");
                l_tableModel.addColumn("OH Dagger");
                l_tableModel.addColumn("OH Fist");
                l_tableModel.addColumn("OH Mace");
                l_tableModel.addColumn("OH Sword");
            }
        }
    }

    private void populateTable()
    {
        CItemChooserTableModel l_tableModel = (CItemChooserTableModel) this.itemChooserTable.getModel();
        l_tableModel.resetColumnData();
        int l_column = 0;

        if (m_chooserType == ChooserType.META)
        {
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.META),l_column++);

        } else if (m_chooserType == ChooserType.GEM)
        {
            // Add the data
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.RED, m_gemQualityFilter),l_column++);
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.BLUE, m_gemQualityFilter),l_column++);
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.GREEN, m_gemQualityFilter),l_column++);
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.ORANGE, m_gemQualityFilter),l_column++);
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.PURPLE, m_gemQualityFilter),l_column++);
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.YELLOW, m_gemQualityFilter),l_column++);
            l_tableModel.addColumnData(CGlobal.g_database.getListForGems(CGemType.GemType.PRISMATIC, m_gemQualityFilter),l_column++);
        } else if (m_chooserType == ChooserType.POISON)
        {
            if (m_slotType == DollSlotType.MAINHAND || m_slotType == DollSlotType.OFFHAND)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getPoisonList(), l_column++);
            }
        } else if (m_chooserType == ChooserType.ENCHANT)
        {
            if (m_slotType == DollSlotType.HEAD)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.HEAD), l_column++);
            } else if (m_slotType == DollSlotType.SHOULDERS)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.SHOULDERS), l_column++);
            } else if (m_slotType == DollSlotType.BACK)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.BACK), l_column++);
            } else if (m_slotType == DollSlotType.CHEST)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.CHEST), l_column++);
            } else if (m_slotType == DollSlotType.WRIST)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.WRIST), l_column++);
            } else if (m_slotType == DollSlotType.HANDS)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.HANDS), l_column++);
            } else if (m_slotType == DollSlotType.WAIST)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.WAIST), l_column++);
            } else if (m_slotType == DollSlotType.LEGS)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.LEGS), l_column++);
            } else if (m_slotType == DollSlotType.FEET)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.FEET), l_column++);
            } else if (m_slotType == DollSlotType.FINGER1 ||
                       m_slotType == DollSlotType.FINGER2)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.FINGER1), l_column++);
            } else if (m_slotType == DollSlotType.MAINHAND ||
                       m_slotType == DollSlotType.OFFHAND)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getEnchantListBySlot(ItemSlot.MAINHAND), l_column++);
            }
        } else if (m_chooserType == ChooserType.ITEM)
        {
            if (m_slotType == DollSlotType.HEAD)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.HEAD, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.NECK)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.NECK, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.SHOULDERS)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.SHOULDERS, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.BACK)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.BACK, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.CHEST)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.CHEST, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.WRIST)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.WRIST, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.HANDS)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.HANDS, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.WAIST)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.WAIST, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.LEGS)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.LEGS, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.FEET)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.FEET, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.FINGER1 ||
                       m_slotType == DollSlotType.FINGER2)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.FINGER, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.TRINKET1 ||
                       m_slotType == DollSlotType.TRINKET2)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListBySlot(ItemSlot.TRINKET, m_itemQualityFilter), l_column++);
            } else if (m_slotType == DollSlotType.RANGED)
            {
                l_tableModel.addColumnData(CGlobal.g_database.getListByType(ItemType.BOW, m_itemQualityFilter),l_column++);
                l_tableModel.addColumnData(CGlobal.g_database.getListByType(ItemType.CROSSBOW, m_itemQualityFilter),l_column++);
                l_tableModel.addColumnData(CGlobal.g_database.getListByType(ItemType.GUN, m_itemQualityFilter),l_column++);
                l_tableModel.addColumnData(CGlobal.g_database.getListByType(ItemType.THROWN, m_itemQualityFilter),l_column++);

            } else if (m_slotType == DollSlotType.MAINHAND)
            {
                ArrayList<CItem> l_arrayList;
                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.AXE, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.AXE, ItemSlot.MAINHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.DAGGER, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.DAGGER, ItemSlot.MAINHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.FIST, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.FIST, ItemSlot.MAINHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.MACE, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.MACE, ItemSlot.MAINHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.SWORD, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.SWORD, ItemSlot.MAINHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);
            } else if (m_slotType == DollSlotType.OFFHAND)
            {
                ArrayList<CItem> l_arrayList;
                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.AXE, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.AXE, ItemSlot.OFFHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.DAGGER, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.DAGGER, ItemSlot.OFFHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.FIST, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.FIST, ItemSlot.OFFHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.MACE, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.MACE, ItemSlot.OFFHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);

                l_arrayList = CGlobal.g_database.getListByTypeAndSlot(ItemType.SWORD, ItemSlot.ONEHAND, m_itemQualityFilter);
                l_arrayList.addAll(CGlobal.g_database.getListByTypeAndSlot(ItemType.SWORD, ItemSlot.OFFHAND, m_itemQualityFilter));
                Collections.sort(l_arrayList, new CItemComparatorQualityAlpha());
                l_tableModel.addColumnData(l_arrayList, l_column++);
            }
        }

        l_tableModel.fireTableDataChanged();
        l_tableModel.setWidth(itemChooserTable, 230);
    }

    public CItem getValue()
    {
        this.setVisible(true);
        return m_itemChosen;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        itemChooserTable = new javax.swing.JTable();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        legendaryCheckBox = new javax.swing.JCheckBox();
        epicCheckBox = new javax.swing.JCheckBox();
        rareCheckBox = new javax.swing.JCheckBox();
        uncommonCheckBox = new javax.swing.JCheckBox();
        commonCheckBox = new javax.swing.JCheckBox();
        setToNoneButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                windowClosingHandler(evt);
            }
        });

        itemChooserTable.setModel(new CItemChooserTableModel());
        itemChooserTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        itemChooserTable.setCellSelectionEnabled(true);
        itemChooserTable.setColumnSelectionAllowed(true);
        itemChooserTable.setRowSelectionAllowed(true);
        itemChooserTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itemChooserTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(itemChooserTable);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Filter Quality", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        legendaryCheckBox.setForeground(new java.awt.Color(204, 102, 0));
        legendaryCheckBox.setText("Legendary");
        legendaryCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                legendaryCheckBoxItemStateChanged(evt);
            }
        });

        epicCheckBox.setForeground(new java.awt.Color(255, 51, 255));
        epicCheckBox.setText("Epic");
        epicCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                epicCheckBoxItemStateChanged(evt);
            }
        });

        rareCheckBox.setForeground(new java.awt.Color(51, 51, 255));
        rareCheckBox.setText("Rare");
        rareCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rareCheckBoxItemStateChanged(evt);
            }
        });

        uncommonCheckBox.setForeground(new java.awt.Color(0, 153, 0));
        uncommonCheckBox.setText("UnCommon");
        uncommonCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                uncommonCheckBoxItemStateChanged(evt);
            }
        });

        commonCheckBox.setForeground(new java.awt.Color(102, 102, 102));
        commonCheckBox.setText("Common");
        commonCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                commonCheckBoxItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(legendaryCheckBox)
                .addGap(18, 18, 18)
                .addComponent(epicCheckBox)
                .addGap(29, 29, 29)
                .addComponent(rareCheckBox)
                .addGap(27, 27, 27)
                .addComponent(uncommonCheckBox)
                .addGap(18, 18, 18)
                .addComponent(commonCheckBox)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(legendaryCheckBox)
                    .addComponent(epicCheckBox)
                    .addComponent(commonCheckBox)
                    .addComponent(uncommonCheckBox)
                    .addComponent(rareCheckBox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setToNoneButton.setText("Set to None");
        setToNoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setToNoneButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(setToNoneButton)
                        .addGap(31, 31, 31)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(setToNoneButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void windowClosingHandler(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosingHandler
        m_itemChosen = null;
        dispose();
    }//GEN-LAST:event_windowClosingHandler

    private void legendaryCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_legendaryCheckBoxItemStateChanged
        updateFilter(this.legendaryCheckBox, ItemQuality.LEGENDARY);
    }//GEN-LAST:event_legendaryCheckBoxItemStateChanged

    private void epicCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_epicCheckBoxItemStateChanged
        updateFilter(this.epicCheckBox, ItemQuality.EPIC);
    }//GEN-LAST:event_epicCheckBoxItemStateChanged

    private void rareCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rareCheckBoxItemStateChanged
        updateFilter(this.rareCheckBox, ItemQuality.RARE);
    }//GEN-LAST:event_rareCheckBoxItemStateChanged

    private void uncommonCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_uncommonCheckBoxItemStateChanged
        updateFilter(this.uncommonCheckBox, ItemQuality.UNCOMMON);
    }//GEN-LAST:event_uncommonCheckBoxItemStateChanged

    private void commonCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_commonCheckBoxItemStateChanged
        updateFilter(this.commonCheckBox, ItemQuality.COMMON);
    }//GEN-LAST:event_commonCheckBoxItemStateChanged

    private void itemChooserTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_itemChooserTableMouseClicked
        if (evt.getClickCount() > 1)
        {
            int row = itemChooserTable.rowAtPoint(new Point(evt.getX(), evt.getY()));
            int col = itemChooserTable.convertColumnIndexToModel(itemChooserTable.columnAtPoint(new Point(evt.getX(), evt.getY())));
            CItem l_item = (CItem) itemChooserTable.getModel().getValueAt(row, col);
            if (l_item != null)
            {
                m_itemChosen = l_item;
                this.dispose();
            }
        }
    }//GEN-LAST:event_itemChooserTableMouseClicked

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        m_itemChosen = null;
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void setToNoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setToNoneButtonActionPerformed
        m_itemChosen = CGlobal.g_database.getItem("0");
        this.dispose();
}//GEN-LAST:event_setToNoneButtonActionPerformed


    private void updateFilter(JCheckBox p_checkBox, ItemQuality p_qualityFilter)
    {
        if (this.m_chooserType == ChooserType.GEM)
        {
            this.m_gemQualityFilter.setFilter(p_qualityFilter, p_checkBox.isSelected());
            this.populateTable();
        } else if (this.m_chooserType == ChooserType.ITEM)
        {
            this.m_itemQualityFilter.setFilter(p_qualityFilter, p_checkBox.isSelected());
            this.populateTable();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JCheckBox commonCheckBox;
    private javax.swing.JCheckBox epicCheckBox;
    private javax.swing.JTable itemChooserTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox legendaryCheckBox;
    private javax.swing.JCheckBox rareCheckBox;
    private javax.swing.JButton setToNoneButton;
    private javax.swing.JCheckBox uncommonCheckBox;
    // End of variables declaration//GEN-END:variables

}
